package amit.calc.form.table;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

import amit.calc.form.CalcRowObj;
import amit.calc.form.MainForm;

/**
 * 
 * @author ido
 * 
 */
public class CalcTableModel extends AbstractTableModel {

	private String[] columnNames = {"No", "Balance", "Principal", "Interest",
			"Total"};
	private ArrayList<CalcRowObj> data;
	private double totalPrincipal=0, totalInterest=0, totalTotal=0, totalCashFlow=0 ;
	private int totalRowNo;
	/**
	 * Compute the table data
	 * @param loan
	 * @param years
	 * @param interest
	 * @param rent
	 */
	public CalcTableModel(double loan, int	years, double interest, double rent, double monthCF) {
		data = new ArrayList<CalcRowObj>();
		//int weeksTime = (years * 12);
		double principal =  monthCF;   // loan / weeksTime;
		double tmpInterest = loan * (interest/12);
		double total = principal+tmpInterest;
		CalcRowObj firstObj = new CalcRowObj(1, loan, principal,
				tmpInterest, total, rent - total);
		data.add(firstObj);
		//for (int i = 1; i < weeksTime; i++) {
		int i = 1;
		double balance = Double.MAX_VALUE;
		while ( (balance-principal) > 0 && i < 500) {
			balance = data.get(i-1).getBalance() - data.get(i-1).getPrincipal();
			tmpInterest = balance * (interest/12);
			//total = principal+tmpInterest; 
			principal = total -  tmpInterest;
			if (principal < 0 ){
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						JOptionPane.showMessageDialog(null, "The Principal is negative!\nYou can't return the investment.", 
								"Warning" , JOptionPane.WARNING_MESSAGE);
					}
				});
				break;
			}
			CalcRowObj tmpObj = new CalcRowObj((i+1), balance, principal,tmpInterest,
					total, rent-total);
			data.add(tmpObj);
			i++;
			// sum the current row
			totalPrincipal += principal;
			totalInterest += tmpInterest;
			totalTotal += total;
			totalCashFlow += (rent-total);
		}
		totalRowNo = i;
		// add the sum for each column
		data.add(new CalcRowObj(0,0, totalPrincipal, totalInterest, totalTotal, totalCashFlow));
	}
	
	public ArrayList<CalcRowObj> getData() {
		return data;
	}

	public int getTotalRowNo() {
		return totalRowNo;
	}

	public double getTotalCashFlow() {
		return totalCashFlow;
	}

	public double getTotalInterest() {
		return totalInterest;
	}

	public double getTotalPrincipal() {
		return totalPrincipal;
	}

	public double getTotalTotal() {
		return totalTotal;
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.size();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		switch (col) {
		case 0:
			return (data.get(row)).getNum();
		case 1:
			return (data.get(row)).getBalance();
		case 2:
			return (data.get(row)).getPrincipal();
		case 3:
			return (data.get(row)).getInterest();
		case 4:
			return (data.get(row)).getTotal();
//		case 5:
//			return (data.get(row)).getCashFlow();
		}
		System.err.println("### invalid col:" + col);
		return "";
	}

	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

}
